﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace HackingSilverlightDotNet.CustomControls
{
    public partial class IconImageButton : UserControl
    {
        public IconImageButton()
        {
            InitializeComponent();
        }

        #region Dependency Properties

        #region // HighLightImg DP

        public string HighLightImg
        {
            get { return (string)GetValue(HighLightImgProperty); }
            set { SetValue(HighLightImgProperty, value); }
        }

        public readonly DependencyProperty HighLightImgProperty = DependencyProperty.Register("HighLightImg", typeof(string), typeof(IconImageButton), new PropertyMetadata(new PropertyChangedCallback(OnHighLightImgChanged)));

        private static void OnHighLightImgChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement ThisElement = DpObj as FrameworkElement;

            if (ThisElement != null)
            {
                IconImageButton ThisClassInstance = (IconImageButton)(ThisElement);
                Image ImageElement = (Image)(ThisClassInstance.FindName("ImageElementBack"));


                ImageElement.Source = new BitmapImage(new Uri(ThisClassInstance.HighLightImg, UriKind.RelativeOrAbsolute));
            }
        }

        #endregion

        #region // GreyImg DP

        public string GreyImg
        {
            get { return (string)GetValue(GreyImgProperty); }
            set { SetValue(GreyImgProperty, value); }
        }

        public readonly DependencyProperty GreyImgProperty = DependencyProperty.Register("GreyImg", typeof(string), typeof(IconImageButton), new PropertyMetadata(new PropertyChangedCallback(OnGreyImgPropertyChanged)));

        private static void OnGreyImgPropertyChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement ThisElement = DpObj as FrameworkElement;

            if (ThisElement != null)
            {
                IconImageButton ThisClassInstance = (IconImageButton)(ThisElement);
                Image ImageElement = (Image)(ThisClassInstance.FindName("ImageElement")); 

                ImageElement.Source = new BitmapImage(new Uri(ThisClassInstance.GreyImg, UriKind.RelativeOrAbsolute));
            }
        }

        #endregion

        #endregion

        private void Image_MouseEnter(object sender, MouseEventArgs e)
        {
            ImageElement.Visibility = Visibility.Collapsed;
            ImageElementBack.Visibility = Visibility.Visible;
        }

        private void Image_MouseLeave(object sender, MouseEventArgs e)
        {
            Reset();

            ImageElement.Visibility = Visibility.Visible;
            ImageElementBack.Visibility = Visibility.Collapsed;
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ImageElementBack.Width = 15;
            ImageElementBack.Height = 15;
            ImageDropShadow1.Opacity = 0.0;
            ImageElementBack.SetValue(Canvas.TopProperty, 3.0);
            ImageElementBack.SetValue(Canvas.LeftProperty, 3.0);
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Reset();
        }

        private void Reset()
        {
            ImageElementBack.Width = 18;
            ImageElementBack.Height = 18;
            ImageDropShadow1.Opacity = 0.7;
            ImageElementBack.SetValue(Canvas.TopProperty, 0.0);
            ImageElementBack.SetValue(Canvas.LeftProperty, 0.0);
        }
    }
}
